/*
 * @Author: szx
 * @Date: 2022-04-06 13:01:53
 * @LastEditTime: 2022-04-06 13:24:14
 * @Description:
 * @FilePath: \leetcode\剑指offerII\041\041.js
 */
/**
 * Initialize your data structure here.
 * @param {number} size
 */
var MovingAverage = function (size) {
    this.nums = [];
    this.capacity = size;
    this.sum = 0;
};

/**
 * @param {number} val
 * @return {number}
 */
MovingAverage.prototype.next = function (val) {
    this.nums.push(val);
    this.sum += val;
    if (this.nums.length > this.capacity) {
        this.sum -= this.nums.shift();
    }
    return this.sum / this.nums.length;
};

/**
 * Your MovingAverage object will be instantiated and called as such:
 * var obj = new MovingAverage(size)
 * var param_1 = obj.next(val)
 */
